home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / primer.arc / PRIMER.C
Text File  |  1988-09-09  |  4KB  |  178 lines

  1. /*    Program PRIMER.C
  2.  
  3. Prime number generator.  Generates MAXNPRIM prime numbers
  4. and prints them in groups of 500.
  5.  
  6. Program by Harry M. Murphy,  1 September 1988.
  7.  
  8. Based on the Pascal prime number generator, PRIMER.PAS
  9. by Harry M. Murphy.  */
  10.  
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include <math.h>
  14.  
  15. /*  Global defined constants. */
  16. #define    LISTSIZE 500
  17. #define    MAXNPRIM 3500
  18. #define    OUTFILE  "PRIMER.LIS"
  19. #define    TBLSIZE  1000
  20.  
  21. /*  Function prototypes. */
  22. void    getprime();
  23. void    page();
  24. void    prntlist();
  25. double    secnds(double);
  26. void    space(int);
  27.  
  28. /*  Global variables.  */
  29. long    inc;
  30. long    list[LISTSIZE+1];    /*  The prime number printing list.  */
  31. int    nprime;
  32. FILE    *out;
  33. int    pge;
  34. long    prime;
  35. long    table[TBLSIZE+1];    /*  The stored prime number table.  */
  36. double    trun;
  37. long    try;
  38. double    tzero;
  39.  
  40. /* ------------------------------ */
  41.  
  42. void    page()
  43.  
  44. {    fprintf(out,"\f\n");
  45.     return; }
  46.  
  47. /* ------------------------------ */
  48.  
  49. double    secnds(double tzero)
  50.  
  51. /*  This function returns the number of seconds since tzero
  52.     as a DOUBLE number.
  53.  
  54.     Requires  #include <dos.h>.
  55.  
  56.     Function by Harry M. Murphy,  9 September 1988.  */
  57.  
  58. {
  59. union    REGS    in,out;
  60. double    secs;
  61.  
  62.     in.h.ah = 0x2c;
  63.     intdos(&in,&out);
  64.     secs = (((((out.h.ch)*60.0 +
  65.             out.h.cl)*60.0 +
  66.             out.h.dh)*100.0 +
  67.             out.h.dl)/100.0) - tzero;
  68.     if (secs < 0.0)
  69.             return secs + 86400.0;
  70.     else
  71.         return secs;  }
  72.  
  73. /* ------------------------------ */
  74.  
  75. void    space(int n)
  76.  
  77. {    int    i;
  78.  
  79.     i = 1;
  80.     while (i <= n) {fprintf(out," "), i++; }
  81.     return; }
  82.  
  83. /* ------------------------------ */
  84.  
  85. void    getprime()
  86.  
  87. {    int    i;
  88.     long    maxdiv;
  89.     int    remain;
  90.     double    tryflt;
  91.  
  92.     do
  93.     {    inc = 6-inc;
  94.         try += inc;
  95.         tryflt = try;
  96.         maxdiv = floor(sqrt(tryflt + 0.5));
  97.         i = 2;
  98.         do
  99.         {    i++;
  100.             remain = try%table[i]; }
  101.         while (remain && (table[i] < maxdiv)); }
  102.     while (!remain);
  103.     prime = try;
  104.     return; }
  105.  
  106. /* ------------------------------ */
  107.  
  108. void    prntlist()
  109.  
  110. {    int    i,j,k;
  111.  
  112.     page();
  113.     pge++;
  114.     printf("Writing page %3d.\r",pge);
  115.     space(71);
  116.     fprintf(out,"Page%5d\n\n",pge);
  117.     fprintf(out,"%8dth -%5dth Primes.",nprime-499,nprime);
  118.     space(36);
  119.     fprintf(out,"%7ld to%7ld\n\n",list[1],list[500]);
  120.     for (i=1; i <= 50; i++)
  121.     {    j = i;
  122.         k = i+450;
  123.         do
  124.         {    fprintf(out,"%8ld",list[j]);
  125.             j = j+50; }
  126.         while (j <= k);
  127.     fprintf(out,"\n"); }
  128.     return; }
  129.  
  130. /* ------------------------------ */
  131.  
  132. void    main()
  133.  
  134. {    int    inlist, intbl;
  135.  
  136.     tzero = secnds(0.0);
  137.     if ((out = fopen(OUTFILE,"w")) == NULL)
  138.         {printf("Can't open %s!",OUTFILE);
  139.         exit(1); }
  140.     printf("Generating %d primes and writing them to %s.\n",
  141.                MAXNPRIM,OUTFILE);
  142.     pge = 0;
  143.     table[0] = 1;
  144.     table[1] = 2;
  145.     table[2] = 3;
  146.     table[3] = 5;
  147.     try = 5;
  148.     inc = 4;
  149.     for (intbl = 4; intbl <= TBLSIZE; intbl++)
  150.     {    getprime();
  151.         table[intbl] = prime; }
  152.     inlist = 0;
  153.     nprime = 0;
  154.     for (intbl = 1; intbl <= TBLSIZE; intbl++)
  155.     {    list[++inlist] = table[intbl];
  156.         nprime++;
  157.         if (inlist == LISTSIZE)
  158.         {    prntlist();
  159.             inlist = 0; } }
  160.     do
  161.     {    getprime();
  162.         list[++inlist] = prime;
  163.         nprime++;
  164.         if (inlist == LISTSIZE)
  165.         {    prntlist();
  166.             inlist = 0; } }
  167.     while ((nprime < MAXNPRIM) | (inlist != 0));
  168.     page();
  169.     fprintf(out,"   End of prime number tables.\n");
  170.     trun = secnds(tzero);
  171.     fprintf(out,"   Run time =%5.1f seconds.\n",trun);
  172.     printf("Run time =%5.1f seconds.\n",trun);
  173.     fclose(out);
  174.     printf("The tables are generated.\n");
  175.     printf("The results are in file %s.",OUTFILE); }
  176.  
  177. /*    End of Program PRIMER.  */
  178.